package com.aglhz.nature.view;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.aglhz.base.BaseActivity;
import com.aglhz.nature.R;
import com.aglhz.nature.fragment.MyselfFragment;
import com.aglhz.nature.presenter.LoginPresenter;
import com.aglhz.nature.view.iv.LoginView;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements LoginView {
	private SharedPreferences sharedPreferences;
	private SharedPreferences shPreferences;
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;
	private String token=null;
	private String NORMAL_Token=null;

	//wei xin denglu
	private static final String Login_Tag="Login_Tag";
	private UMShareAPI mShareAPI = null;
	String openid;
	String refresh_token;
	String access_token;
	String unionid;
	String nickname;
	String headimgurl;
	Map<String, String> wxinfo;
	private String RegistNumber;
	private String RegistPassword;
	private String nor_token;
	private String wx_token;
	private LoginPresenter presenter;

	private static final String LOGININFO = "LOGININFO";
	private TextView goto_register;
	private EditText login_et_name;
	private EditText login_et_password;
	private CheckBox checkBox_remeberPassword;
	private Button login;
	private ImageView wxLogin;
	private int flag=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_phone);
		setActionBarTitle("会员登录");
		
			flag=getIntent().getIntExtra("MyselfFragmentFlag", 0);
		  /** init auth api**/
        mShareAPI = UMShareAPI.get(this);
        
		//判断是否已登录
		preferences =getSharedPreferences("WX_INFO", Context.MODE_PRIVATE);
		if(null==preferences){
			return;
		}else {
			wx_token=preferences.getString("token", "");
		}
		
		//判断普通登陆
		preferences = getSharedPreferences("Normal_Info", Context.MODE_PRIVATE);
		if(null==preferences){
			return;
		}else {
			NORMAL_Token=preferences.getString("NORMAL_Token", "");
		}
		
		
		//设置被判断的值
		if(!(""==wx_token)){
			token=wx_token;
		}else if(!(""==NORMAL_Token)){
			token=NORMAL_Token;
		}
		if(!TextUtils.isEmpty(token)){
			Intent intent =new Intent(LoginActivity.this ,MainActivity.class);
			MainActivity.MAIN_FRAM_TAG=MainActivity.TO_FRAG_MEMBER_TAG;
			startActivity(intent);
			finish();
		}
		//注册成功后取注册数据
		 sharedPreferences = getSharedPreferences("Regist_INFO",Context.MODE_PRIVATE); 
		 RegistNumber=sharedPreferences.getString("RegistNumber", "");
		 RegistPassword=sharedPreferences.getString("RegistPassword", "");

			
		goto_register = (TextView) findViewById(R.id.goto_register);
		login_et_name = (EditText) findViewById(R.id.login_et_name);
		login_et_password = (EditText) findViewById(R.id.login_et_password);
		login = (Button) findViewById(R.id.login);
		wxLogin = (ImageView) findViewById(R.id.weixinlog);
		checkBox_remeberPassword = (CheckBox) findViewById(R.id.checkBox_remeberPassword);
		//设置默认账号
		login_et_name.setText(RegistNumber);
		login_et_password.setText(RegistPassword);
		
		presenter = new LoginPresenter(this,LoginActivity.this);
		// basePresenter register
		presenter.register();

		
		goto_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}
		});

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(!TextUtils.isEmpty(getUser())&&!TextUtils.isEmpty(getPassword())){
					presenter.loadLoginData();

					Log.e("登陆信息","账号密码不为空"+getUser());
					
				}else{
					Toast.makeText(LoginActivity.this, "请输入账号或密码", Toast.LENGTH_LONG)
					.show();
					Log.e("登陆信息","账号密码为空");
				}
				
			}
		});

		wxLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {


				 SHARE_MEDIA platform = null;
				 platform = SHARE_MEDIA.WEIXIN;
				 /**begin invoke umeng api**/

		         mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
			}
		});

	}

	@Override
	public Boolean isChecked() {
		if (checkBox_remeberPassword.isChecked()) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public String getUser() {

		return login_et_name.getText().toString().trim();
		
	}

	@Override
	public String getPassword() {

		return login_et_password.getText().toString().trim();
	}

	@Override
	public void showToast(int code) {
		
		
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			MainActivity.MAIN_FRAM_TAG=MainActivity.TO_FRAG_MEMBER_TAG;
			intent.putExtra("LOGININFO", LOGININFO);
			startActivity(intent);
			finish();
		

	}
	@Override
	public void showLostToast() {
		Toast.makeText(LoginActivity.this, presenter.getLoseInfo(), Toast.LENGTH_SHORT)
		.show();
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		presenter.unregister();
	}

	@Override
	public void saveNormalInfo() {
		// TODO Auto-generated method stub
		// 应该保存
		sharedPreferences = getSharedPreferences("Normal_Info",
				Context.MODE_PRIVATE);
		editor = sharedPreferences.edit();
		
		editor.putString("NORMAL_Token", presenter.saveNormalToken());
		editor.putString("NORMAL_Name", presenter.saveNormalName());
		editor.putString("NORMAL_POINT",presenter.saveNormalIntegral());
		editor.putString("NORMAL_MONEY", presenter.saveNormalNumber());
		Log.e("presenter.saveNormalIntegral()", presenter.saveNormalIntegral());
		//标识普通登陆不为空
		editor.putString("NORMAL_LogInfo","NORMAL_LogInfo");
		// 一定要提交
		Log.e("sharedPreferences", "------------");
		editor.commit();
	}

	@Override
	public void saveEditTextInfo() {
		shPreferences = getSharedPreferences("EditText_Info",
				Context.MODE_PRIVATE);
		editor = shPreferences.edit();
		editor.putString("NORMAL_ACCOUNT", login_et_name.getText().toString().trim());
		editor.putString("NORMAL_PASSWORD", login_et_password.getText().toString().trim());		
		editor.commit();
		Log.e("shPreferences", "------------"+login_et_name.getText().toString().trim());
	}

	
	 /** auth callback interface**/
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
        	//获取access_token
        	openid=data.get("openid");
        	refresh_token=data.get("refresh_token");
        	access_token=data.get("access_token");
        	unionid=data.get("unionid");
        	Log.e("access_token","----------------------"+openid+refresh_token);
        	Log.e("access_token","----------------------"+data.toString());
        	mShareAPI.getPlatformInfo(LoginActivity.this, platform, umAuthUserListener);
            

            
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
        }
    };
  
    
   //用户信息 
    private UMAuthListener umAuthUserListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

            if (data!=null){
                Log.d("auth callbacl","getting data");
               //获取用户信息
                Log.e("获取用户信息", "------------------"+data.toString());
//	            Intent intent =new Intent(LoginActivity.this,MainActivity.class);		            
//	           intent.putExtra("openid",openid);
//	           intent.putExtra("refresh_token",refresh_token);
//	           intent.putExtra("Login_Tag",Login_Tag);
	           
	           nickname=data.get("nickname");
	           headimgurl=data.get("headimgurl");
	           //应该保存
		        sharedPreferences = getSharedPreferences("WX_INFO",Context.MODE_PRIVATE);  
		           editor = sharedPreferences.edit();  
		           editor.putString("refresh_token",refresh_token);
		           editor.putString("openid",openid);
		           editor.putString("access_token",access_token);
		           editor.putString("nickname",nickname);
		           editor.putString("headimgurl",headimgurl);
		           editor.putString("unionid",unionid);
		           // 一定要提交  
		           Log.e("sharedPreferences","------------");
	                editor.commit();  
	          presenter.getUserInfo();
//	           Log.e("tttt","--------------------"+refresh_token+"--------------------"+openid);
//	            startActivity(intent);
//	            finish();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("auth", "on activity re 2");
        mShareAPI.onActivityResult(requestCode, resultCode, data);
        Log.d("auth", "on activity re 3");
    }



	
	@Override
	public void intoIntent() {
		//微信跳转
//		Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG)
//		.show();
		Intent intent = new Intent(LoginActivity.this, MainActivity.class);
		intent.putExtra("Login_Tag", Login_Tag);
		MainActivity.MAIN_FRAM_TAG=MainActivity.TO_FRAG_MEMBER_TAG;
		startActivity(intent);
		finish();
		
	}
	
	@Override
	public String setRefresh_token() {
		return refresh_token;
	}

	@Override
	public String setOpenid() {
		return openid;
	}

	@Override
	public String setAccess_token() {
		return access_token;
	}



	@Override
	public String setUnionid() {
		return unionid;
	}

	@Override
	public String setNickname() {
		return nickname;
	}

	@Override
	public String setHeadimgurl() {
		return headimgurl;
	}

	@Override
	public void saveWXToken() {
		preferences =getSharedPreferences("WX_INFO", Context.MODE_PRIVATE);
		preferences.edit().putString("token", presenter.getToken()).commit();
		
		
	}

	

	




}
